package com.wxsh.english;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

import com.wxsh.util.StorySplit;

public class ExtractEnglishWordsByRegex {

	public static void main(String[] args) throws Exception {
		// 1.分割内容
		StorySplit.splitByChar();
		// 2.匹配单词
		extractWords();
		// 3.过滤熟悉和错误的单词
		filterWords();
	}
	
	private static void extractWords() throws Exception {
		List<String> lines = FileUtils.readLines(new File("D:\\english_split.txt"), "utf-8");
		List<String> destLines = new ArrayList<String>();
		String regex = "^[A-Za-z]{2,}$";
		Pattern ptn = Pattern.compile(regex);
		for (int i = 0; i < lines.size(); i++) {
			String ln = lines.get(i);
			Matcher matcher = ptn.matcher(ln);
			if(matcher.find()){
				destLines.add(ln);
			}
		}
		FileUtils.writeLines(new File("D:\\english_extract_words.txt"), "utf-8", destLines, false);
	}
	
	private static void filterWords() throws Exception {
		List<String> shuLines = FileUtils.readLines(new File("C:\\Users\\Administrator\\git\\EnglishWords\\熟单词表.txt"), "utf-8");
		List<String> shengLines = FileUtils.readLines(new File("C:\\Users\\Administrator\\git\\EnglishWords\\生单词表.txt"), "utf-8");
		List<String> lines = FileUtils.readLines(new File("D:\\english_extract_words.txt"), "utf-8");
		Set<String> destLines = new LinkedHashSet<String>();
		for (int i = 0; i < lines.size(); i++) {
			String ln = lines.get(i).toLowerCase();
			if(shuLines.contains(ln)&&!shengLines.contains(ln)){
				
			}else{
				destLines.add(ln);
			}
		}
		FileUtils.writeLines(new File("D:\\english_words.txt"), "utf-8", destLines, false);
	}

}
